let { insuresModel } = require('./models/insuresModel')


//获取所有的保险产品
module.exports.getInsures = async function ({ pageSize, currentPage }) {
    let total = await insuresModel.countDocuments();
    let data = await insuresModel.find().populate('typesId').limit(pageSize - 0).skip((currentPage - 1) * pageSize)
    return { total, data }
}
// module.exports.getInsures = async function (result) {
//     console.log(3333, result)
//     return await insuresModel.find().populate('typesId')
// }

//搜索
module.exports.searchInsures = async function ({ pageSize, currentPage, type, insearchInsuranceValue }) {
    let total = await insuresModel.countDocuments({
        //模糊查询
        [type]: { $regex: insearchInsuranceValue, $options: '$i' }
    });
    let data = await insuresModel.find({ [type]: { $regex: insearchInsuranceValue, $options: '$i' } }).populate('typesId').limit(pageSize - 0).skip((currentPage - 1) * pageSize)
    return { total, data }
    // return await insuresModel.find({ [type]: { $regex: insearchInsuranceValue, $options: '$i' } }).populate('typesId')
}

//新增
module.exports.addInsures = async function (insuresInfo) {
    return await insuresModel.create(insuresInfo)
}

//删除
module.exports.deleteInsures = async function (delID) {
    return await insuresModel.deleteOne(delID)
}

//修改
module.exports.updateInsures = async function (updateInfo) {
    console.log(updateInfo, 3333);
    let { _id, productsName, payType, renew, policy, suitableFor, price, typesId } = updateInfo
    return await insuresModel.updateOne({ _id }, { productsName, payType, renew, policy, suitableFor, price, typesId })
}